﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmPickUpGoods : BaseForm
    {
        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();

        public FrmPickUpGoods()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void FrmPickUpGoods_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            this.openStarDate.Value = dt.Date.AddDays(-11);
            //提货仓初始化
            this.cBoxShop.ValueMember = "SHOP_ID";
            this.cBoxShop.DisplayMember = "SHOP";
            this.cBoxShop.DataSource = shopBLL.GetAll();
            // this.cBoxShop.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            DateTime dt = DateTime.Now;
            this.openEndDate.Value = dt;
            this.openStarDate.Value = dt.Date.AddDays(-11);
            this.txtDepCode.Text = accountUser.DeptCode;
            this.chkF.Clear();

            this.txtOperUser.Text = accountUser.UserCode;
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.txtShopCode.Text = "01";
            this.cBoxShop.SelectedValue = this.txtShopCode.Text;
            this.dataGridView1.Rows.Clear();
            this.chkF.Text = "Y";

            this.openEndDate.Enabled = true;
            this.openStarDate.Enabled = true;
            this.txtShopCode.Enabled = true;
            this.cBoxShop.Enabled = true;
            this.txtDepCode.Enabled = true;
            this.chkF.Enabled = true;

            this.txtNo.Clear();
            this.txtSm1.Clear();
            this.txtSm2.Clear();
            this.txtKd.Clear();
            this.txtTh.Clear();
            this.txtKp.Clear();
            this.txtSk.Clear();
            this.txtZw.Clear();
            this.txtBei.Clear();
            this.txtTotalNum.Clear();
            this.txtTotalQuantity.Clear();
            this.txtSumJe.Clear();
            this.txtNoSumJe.Clear();

            this.btnAdd.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnQuit.Enabled = false;
            this.btnCan.Enabled = true;

            this.txtShopCode.Focus();
        }

        private void cBoxShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxShop.SelectedValue != null)
                this.txtShopCode.Text = this.cBoxShop.SelectedValue.ToString().Trim();
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
                {
                    this.cBoxShop.SelectedValue = this.txtShopCode.Text.Trim().ToUpper();
                    if (this.cBoxShop.SelectedValue == null)
                    {
                        MessageBox.Show("提货仓代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtShopCode.Focus();
                    }
                    else
                        this.txtDepCode.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                }

            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    this.chkF.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                }

            }
        }

        private void chkF_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtShopCode.Text.Trim()) || this.cBoxShop.SelectedValue == null ||
                    !this.txtShopCode.Text.Trim().Equals(this.cBoxShop.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show("提货仓代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text);
                    if (depart == null)
                    {
                        MessageBox.Show("部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtDepCode.Focus();
                        return;
                    }
                }
                if (string.IsNullOrEmpty(this.chkF.Text.Trim()))
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.chkF.Focus();
                    return;
                }
                else if (!this.chkF.Text.Trim().ToUpper().Equals("Y") && !this.chkF.Text.Trim().ToUpper().Equals("N"))
                {
                    MessageBox.Show("应置为：Y(是)或N(否)！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.chkF.Focus();
                    return;
                }


                this.openStarDate.Enabled = false;
                this.openEndDate.Enabled = false;
                this.txtShopCode.Enabled = false;
                this.cBoxShop.Enabled = false;
                this.txtDepCode.Enabled = false;
                this.chkF.Enabled = false;

                this.txtNo.Enabled = true;
                this.txtNo.Focus();
                this.btnDelRecord.Enabled = true;
                this.btnAddRecord.Enabled = true;
                this.btnSave.Enabled = true;
            }
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtNo.Text.Trim()))
                {
                    MessageBox.Show("请输入欲确认的单据编号！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtNo.Focus();
                    return;
                }
                IList<MisEntity.Sacf> sacfList = sacfBLL.FindAll(this.txtNo.Text.Trim(), this.txtDepCode.Text.Trim());
                if (sacfList == null || sacfList.Count<=0)
                {
                    MessageBox.Show("无此单据号！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtNo.Focus();
                    return;
                }
                MisEntity.Sacf sacf = sacfList[0];
                String shopCode = sacf.SHOP_ID;
                if (!shopCode.Equals(this.cBoxShop.SelectedValue.ToString().Trim()))
                {
                    MessageBox.Show(string.Format("此单据提货仓为{0}，不可操作！", shopCode), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtNo.Focus();
                    return;
                }
                if (sacf.F2.ToString().Equals("1"))
                {
                    MessageBox.Show("此单据已确认，不可操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtNo.Focus();
                    return;
                }
                this.txtSm1.Text=string.Format("{0}-{1}-{2}", sacf.CUS_ID, sacf.CUS_DEP, sacf.CUS_SHOP);
                decimal jeFs=0;
                decimal jeHs=0;
                int slP=0;
                short tax=0;
                foreach (MisEntity.Sacf L_Sacf in sacfList)
                {
                    jeFs += (decimal)L_Sacf.JE_FS;
                    jeHs += (decimal)L_Sacf.JE_HS;
                    slP += (int)L_Sacf.SL_P;
                    if (tax < L_Sacf.TAX) tax = (short)L_Sacf.TAX;
                }
                this.txtSm2.Text = string.Format("库:{0} 未税额:{1} 含税额:{2} 件数:{3} 税率:{4}", shopCode, jeFs, jeHs, slP, tax);
                this.txtKd.Text = string.Format("{0:f}", sacf.DATE);
                this.txtTh.Text = string.Format("{0:f}", sacf.D_TH);
                this.txtKp.Text = string.Format("{0:f}", sacf.D_KP);
                this.txtSk.Text = string.Format("{0:f}", sacf.D_JS);
                this.txtZw.Text = this.chkF.Text.Trim();
                this.txtTotalNum.Text = "0";
                this.txtTotalQuantity.Text = "0";
                this.txtSumJe.Text = "0";
                this.txtNoSumJe.Text = "0";

                this.txtZw.Enabled = true;
                this.txtBei.Enabled = true;

                this.txtZw.Focus();
            }
        }

        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                MessageBox.Show("请输入合法的单据号！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtNo.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtSm1.Text.Trim()))
            {
                MessageBox.Show("请先确认单据号！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtNo.Focus();
                return;
            }
            IList<MisEntity.Sacf> sacfList = sacfBLL.FindAll(this.txtNo.Text.Trim(), this.txtDepCode.Text.Trim());
            if (sacfList == null || sacfList.Count<=0)
            {
                MessageBox.Show("无此单据号，请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtNo.Focus();
                return;
            }
            String no=this.txtNo.Text.Trim();
            int rowCount = this.dataGridView1.RowCount;
            if (rowCount >= 16)
            {
                MessageBox.Show("每单条数不可超过16条！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtNo.Focus();
                return;
            }
            if (rowCount > 0)
            {
                DataGridViewRow dr = null;
                for (int i = 0; i < rowCount; i++)
                {
                    dr = this.dataGridView1.Rows[i];
                    if (no.Equals(dr.Cells["invoice"].Value.ToString()))
                    {
                        MessageBox.Show("单据不可重复！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtNo.Focus();
                        return;
                    }
                }
            }
            String txtZw = this.txtZw.Text.Trim();
            if (!txtZw.ToUpper().Equals("Y"))
                txtZw = "N";
            String[] DataRow = new String[7];
            DataRow[0] = (rowCount+1).ToString();
            DataRow[1] = no;
            DataRow[2] = txtZw;
            DataRow[3] = this.txtKd.Text.Trim();
            DataRow[4] = this.txtSm1.Text.Trim();
            DataRow[5] = this.txtSm2.Text.Trim();
            this.dataGridView1.Rows.Add(DataRow);

            this.txtNo.Clear();
            this.txtSm1.Clear();
            this.txtSm2.Clear();
            this.txtKd.Clear();
            this.txtTh.Clear();
            this.txtKp.Clear();
            this.txtSk.Clear();

            this.txtNo.Focus();
        }

        private void btnDelRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.dataGridView1.Rows.Remove(this.dataGridView1.CurrentRow);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请检查各项目是否正确。\n是否保存[Y/N]？", "提问窗", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (this.dataGridView1.Rows.Count > 0)
                {
                    IList<MisEntity.Sacf> list = new List<MisEntity.Sacf>(this.dataGridView1.Rows.Count);
                    MisEntity.Sacf entity = null;
                    DataGridViewRow dr = null;
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        dr = this.dataGridView1.Rows[i];
                        IList<MisEntity.Sacf> sacfList = sacfBLL.FindAll(dr.Cells["invoice"].Value.ToString(), this.txtDepCode.Text.Trim());
                        if (sacfList != null && sacfList.Count > 0)
                        {
                            foreach (MisEntity.Sacf sacf in sacfList)
                            {
                                entity = new MisEntity.Sacf();
                                entity.NO = dr.Cells["invoice"].Value.ToString();
                                entity.NONO = sacf.NONO;
                                entity.ID = sacf.ID;
                                entity.DEP_ID = this.txtDepCode.Text.Trim();
                                entity.BEI = dr.Cells["confirm"].Value.ToString();
                                entity.OP_TH = accountUser.UserCode;
                                entity.DAY_JS = sacf.DAY_JS;
                                list.Add(entity);
                            }
                        }

                    }
                    sacfBLL.Confirm(list);
                    this.btnSave.Enabled = false;
                    this.btnCan.Enabled = false;
                    MessageBox.Show("已存盘！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    this.txtNo.Enabled = false;
                    this.txtZw.Enabled = false;
                    this.txtBei.Enabled = false;

                    this.btnAdd.Enabled = true;
                    this.btnAddRecord.Enabled = false;
                    this.btnDelRecord.Enabled = false;
                    this.btnPrint.Enabled = true;
                    this.btnQuit.Enabled = true;
                }
            }
        }

        private void txtZw_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBei.Focus();
            }
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAddRecord.Focus();
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.openStarDate.Enabled = false;
            this.openEndDate.Enabled = false;
            this.txtShopCode.Enabled = false;
            this.cBoxShop.Enabled = false;
            this.txtDepCode.Enabled = false;
            this.chkF.Enabled = false;

            this.txtNo.Enabled = false;
            this.txtZw.Enabled = false;
            this.txtBei.Enabled = false;

            this.btnSave.Enabled = false;
            this.btnCan.Enabled = false;
            this.btnAdd.Enabled = true;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = true;
            this.btnQuit.Enabled = true;
        }
    }
}
